package UI;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ScoreFrame extends JFrame {
	private JPanel panel = new JPanel();
	private int score;
	private JLabel label2;
	private JLabel label3;
	private JTextArea textarea;
	private JButton button1;
	private JButton button2;
	private JButton button3;
	String str = new String("");

	private Clientcontext client;

	public ScoreFrame() {
		super();
		JFrame m = this;
		init();
		m.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		m.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				ExitJDialoge a = new ExitJDialoge(m);
				a.button1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						System.exit(0);
					}
				});
				a.button2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						a.setVisible(false);
					}
				});
				a.setVisible(true);
			}
		});
	}

	public void init() {
		this.add(panel);
		Label();
		button();
		panel.setLayout(null);
		this.setSize(1200, 800);
		this.setTitle("神奇宝贝大师在线测评");
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void button() {
		button1 = new JButton("返回菜单");
		panel.add(button1);
		button1.setFont(new java.awt.Font("微软雅黑", 1, 30));
		button1.setForeground(Color.BLACK);
		button1.setLocation(100, 600);
		button1.setSize(250, 80);
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				client.setscore();
			}
		});
		button2 = new JButton("查看成绩表");
		panel.add(button2);
		button2.setFont(new java.awt.Font("微软雅黑", 1, 30));
		button2.setForeground(Color.BLACK);
		button2.setLocation(450, 600);
		button2.setSize(250, 80);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				scoretable();
			}
		});
	}

	public void scoretable() {
		JFrame jf = new JFrame();
		JPanel jp = new JPanel();
		jf.add(jp);
		jp.add(textarea);
		textarea.setText(client.score());
		jf.setLocation(500, 200);
		jf.setSize(800, 600);
		jf.setTitle("神奇宝贝大师在线测评");
		jf.setVisible(true);
	}

	public void Label() {
		JLabel label1 = new JLabel("你的成绩是");
		label2 = new JLabel();
		label3 = new JLabel();
		panel.add(label1);
		label1.setFont(new java.awt.Font("微软雅黑", 1, 100));
		label1.setForeground(Color.BLACK);
		label1.setLocation(340, 10);
		label1.setSize(1000, 120);
		panel.add(label2);
		label2.setFont(new java.awt.Font("微软雅黑", 1, 100));
		label2.setForeground(Color.BLACK);
		label2.setLocation(340, 300);
		label2.setSize(1000, 120);
		panel.add(label3);
		label3.setFont(new java.awt.Font("微软雅黑", 1, 50));
		label3.setForeground(Color.BLACK);
		label3.setLocation(600, 260);
		label3.setSize(1000, 200);
	}

	public void setscore(int score) {
		this.score = score;
		String sc = String.valueOf(score);
		String sb = sc + "        分\n";
		String sn = "姓名:        " + client.getName();
		str = sn + "            " + sb;
		textarea = new JTextArea();
		textarea.setFont(new java.awt.Font("微软雅黑", 1, 25));
		textarea.setForeground(Color.BLACK);
		textarea.setOpaque(false);
		label2.setText(sc);
	}

	public String getStr() {
		return str;
	}

	public void nullscore() {
		label2.setText("暂无成绩");
	}

	public void img() {
		if (score < 60) {
			img1();
		} else if (score <= 80 && score > 60) {
			img2();
		} else if (score < 100 && score > 80) {
			img3();
		} else if (score == 100) {
			img4();
		}
	}

	public void img1() {
		ImageIcon icon = new ImageIcon(getClass().getResource("d.gif"));
		icon.getImage().getScaledInstance(400, 400, Image.SCALE_SMOOTH);
		label3.setIcon(icon);
		label3.setText("倔强青铜");
	}

	public void img2() {
		ImageIcon icon = new ImageIcon(getClass().getResource("e.gif"));
		icon.getImage().getScaledInstance(400, 400, Image.SCALE_SMOOTH);
		label3.setIcon(icon);
		label3.setText("尊贵铂金");
	}

	public void img3() {
		ImageIcon icon = new ImageIcon(getClass().getResource("f.gif"));
		icon.getImage().getScaledInstance(400, 400, Image.SCALE_SMOOTH);
		label3.setIcon(icon);
		label3.setText("超凡大师");
	}

	public void img4() {
		ImageIcon icon = new ImageIcon(getClass().getResource("g.gif"));
		icon.getImage().getScaledInstance(400, 400, Image.SCALE_SMOOTH);
		label3.setIcon(icon);
		label3.setText("最强王者");
	}

	public void setClient(Clientcontext client) {
		this.client = client;
	}

}
